<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
  <div class="layui-form" lay-filter="form_online">
    <div class="layui-form-item">
      <label class="layui-form-label">商品名称</label>
      <div class="layui-input-inline">
        <input type="text" class="layui-input" lay-verify="required" name="name"
               value="{{d.params.name || ''}}">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">指定代理</label>
      <div class="layui-input-inline">
        <select name="agent_id" lay-filter="agent_id" lay-verify="required">
          <option value="">请指定代理商</option>
          {{# layui.each(d.params.agents, function(index, agent){ }}
          <option value="{{agent.id}}" {{d.params.agent_id===agent.id?'selected':''}}>{{agent.name}}</option>
          {{# }) }}
        </select>
      </div>
      <div class="layui-input-inline">
        <select name="category_id" lay-filter="category_id" lay-verify="required">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label"></label>
      <div class="layui-input-block">
        <input type="checkbox" name="test_only" lay-skin="primary" title="仅测试用户可见" {{d.params.test_only?'checked':''}}>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">销售库存</label>
      <div class="layui-input-inline">
        <input type="text" class="layui-input" lay-verify="required" name="left_count"
               value="{{d.params.left_count || 0}}">
      </div>
      <div class="layui-inline layui-word-aux layui-form-mid">
        库存不能大于 {{d.params.available_tokens_count}}
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">权益类型</label>
      <div class="layui-input-block">
        {{# let r_type = d.params.rights_type.value }}
        <input type="radio" name="rights_type" value="none" title="无权益" {{r_type==='none'?'checked':''}}>
        <input type="radio" name="rights_type" value="key" title="权益兑换码" {{r_type==='key'?'checked':''}}>
        <input type="radio" name="rights_type" value="entity" title="实物邮寄" {{r_type==='entity'?'checked':''}}>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">销售价格</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" lay-verify="required" name="display_price"
                 value="{{d.params.display_price || ''}}">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">限购数量</label>
        <div class="layui-input-inline" style="width: 50px">
          <input type="text" class="layui-input" lay-verify="required" name="buy_limit"
                 value="{{d.params.buy_limit || -1}}">
        </div>
        <div class="layui-input-inline layui-word-aux layui-form-mid">
          -1代表不限制
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-inline">
          <input type="text" id="start_time" class="layui-input" name="start_time" placeholder="默认上架时开始"
                 autocomplete="off" value="{{d.params.start_time || ''}}">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-inline">
          <input type="text" id="end_time" class="layui-input" name="end_time" placeholder="默认下架时结束"
                 autocomplete="off" value="{{d.params.end_time || ''}}">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label form-required">封面图片</label>
        <div class="layui-input-inline">
          <div id="logo_url"></div>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">资产凭证</label>
        <div class="layui-input-block">
          <div id="asset_image_url"></div>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">作者</label>
      <div class="layui-input-inline">
        <input type="text" class="layui-input" name="author"
               value="{{d.params.author || ''}}">
      </div>
      <div class="layui-input-inline layui-word-aux layui-form-mid">
        登记作品的作者信息
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品简介</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" name="intro">{{d.params.intro || ''}}</textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品详情</label>
      <div class="layui-input-inline" style="width: 600px">
        <div id="description">
          {{d.params.description || ''}}
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-footer">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
          <input type="button" lay-submit lay-filter="submit_online" value="确认" class="layui-btn">
        </div>
      </div>
    </div>
  </div>
</script>
<script>
  layui.data.sendParams = function (obj) {
    layui.use(['fox_upload', 'wangEditor'], function () {
      let $ = layui.$
        , fox_upload = layui.fox_upload
        , form = layui.form
        , admin = layui.admin
        , editor = layui.wangEditor
        , laydate = layui.laydate

      form.render()

      form.on('select(agent_id)', function (d) {
        loadAgentCategories(d.value)
      })
      if (obj.agent_id) {
        loadAgentCategories(obj.agent_id)
      }

      //日历初始化
      laydate.render({
        elem: '#start_time'
        , type: 'datetime'
      });
      laydate.render({
        elem: '#end_time'
        , type: 'datetime'
      });


      //上传控件初始化
      fox_upload.render({
        elem: '#logo_url'
        , url: 'admin/products/image'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , text: '点击上传'
        , name: 'logo_url'
        , value: obj.logo_url
      })
      fox_upload.render({
        elem: '#asset_image_url'
        , url: 'admin/products/image'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , text: '点击上传'
        , name: 'asset_image_url'
        , value: obj.asset_image_url
      })

      //编辑器
      editor.render({
        elem: "#description"
        , height: 500
        , name: 'description'
        , url: 'editor/images'
      });

      function loadAgentCategories(agentId) {
        let optCategories = $('select[name="category_id"]')
        optCategories.empty()
        optCategories.append(new Option('请选择商品分类', ''))
        if (!agentId) {
          return
        }
        admin.get('admin/agents/' + agentId + '/categories', function (res) {
          layui.each(res.data, function (index, category) {
            if (category.children.length > 0) {
              let optGroup = document.createElement('OPTGROUP')
              optGroup.label = category.name
              layui.each(category.children, function (index2, child) {
                let opt = new Option(child.name, child.id)
                if (obj.category_id == child.id) {
                  opt.selected = true
                }
                optGroup.append(opt)
              })
              optCategories.append(optGroup)
            } else {
              let opt = new Option(category.name, category.id)
              if (obj.category_id == category.id) {
                opt.selected = true
              }
              optCategories.append(opt)
            }
          })
          form.render()
        })
      }
    })
  }
</script>
